#include <gtest/gtest.h>
#include "task/context.h"

TEST(test_task_context, test_task_context_set_read_int)
{
    std::string name = "abc";
    int value = 987;
    task::Context context;
    context.setData(name, value);
    EASE_INFO("set data: %d", value);

    int data = context.data<int>(name, 123);
    EASE_INFO("get data: %d", data);
    ASSERT_EQ(data, value);

    int data2 = 0;
    if (!context.getData(name, data2))
    {
        ASSERT_FALSE(true);
    }
    else
    {
        ASSERT_EQ(data2, value);
    }
}

TEST(test_task_context, test_task_context_set_read_string)
{
    std::string name = "abc";
    std::string value = "987";
    task::Context context;
    context.setData(name, value);
    EASE_INFO("set data: %s", value.c_str());

    auto data = context.data<std::string>(name, "123");
    EASE_INFO("get data: %s", data.c_str());
    ASSERT_EQ(data, value);

    std::string data2;
    if (!context.getData(name, data2))
    {
        ASSERT_FALSE(true);
    }
    else
    {
        ASSERT_EQ(data2, value);
    }
}

class TestDataType
{
public:
    int getAge() const
    {
        return age;
    }
    void setAge(int age)
    {
        TestDataType::age = age;
    }
    std::string getName() const
    {
        return name;
    }
    void setName(const std::string &name)
    {
        TestDataType::name = name;
    }
private:
    int age;
    std::string name;
};

TEST(test_task_context, test_task_context_set_read_class)
{
    std::string name = "key";
    TestDataType testDataType;
    testDataType.setName("sherlock");
    testDataType.setAge(99);

    task::Context context;
    context.setData(name, testDataType);

    auto data = context.data<TestDataType>(name, {});
    ASSERT_EQ(data.getName(), "sherlock");
    ASSERT_EQ(data.getAge(), 99);

    TestDataType data2;
    if (!context.getData(name, data2))
    {
        ASSERT_FALSE(true);
    }
    else
    {
        ASSERT_EQ(data2.getName(), "sherlock");
        ASSERT_EQ(data2.getAge(), 99);
    }
}
